package cn.com.digitalnet.ad.util;


import java.util.List;

import org.apache.commons.lang.CharUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import com.google.common.collect.Lists;



@Component
public class LogUtil {
	
	//记录数据量表的前缀
	static String p = "";
	/**
	 * 对象属性转换为字段  例如：userName to user_name
	 * @param property 字段名
	 * @return
	 */
	public String propertyToField(String property) {
		if (null == property) {
			return "";
		}
		char[] chars = property.toCharArray();
		StringBuffer sb = new StringBuffer();
		for (char c : chars) {
			if (CharUtils.isAsciiAlphaUpper(c)) {
				sb.append("_" + StringUtils.lowerCase(CharUtils.toString(c)));
			} else {
				sb.append(c);
			}
		}
		return sb.toString();
	}

	/**
	 * 字段转换成对象属性 例如：user_name to userName
	 * @param field
	 * @return
	 */
	public String fieldToProperty(String field) {
		if (null == field) {
			return "";
		}
		char[] chars = field.toCharArray();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < chars.length; i++) {
			char c = chars[i];
			if (c == '_') {
				int j = i + 1;
				if (j < chars.length) {
					sb.append(StringUtils.upperCase(CharUtils.toString(chars[j])));
					i++;
				}
			} else {
				sb.append(c);
			}
		}
		return sb.toString();
	}
	

	public String classToTable(String className){
		if (StringUtils.isEmpty(className)) {
			return "";
		}
		StringBuffer sb = new StringBuffer("tbl");
		char[] chars = className.toCharArray();
		for (char c : chars) {
			if (CharUtils.isAsciiAlphaUpper(c)) {
				sb.append("_" + StringUtils.lowerCase(CharUtils.toString(c)));
			} else {
				sb.append(c);
			}
		}
		return sb.toString();

	}
	
	public List<String> classesToTables(List<String> classNames){
		List<String> tableNames = Lists.newArrayList();
		for(String className : classNames){
			tableNames.add(classToTable(className));
		}
		return tableNames;
	}

	
	public String getGetter(String property) {
		char c = property.charAt(0);

		String s = StringUtils.upperCase(CharUtils.toString(c));
		StringBuilder sb = new StringBuilder();
		sb.append("get").append(s).append(property.substring(1));
		return sb.toString();
	}
	
	/*//通过表取得它 所有标记为记录的 属性名称
	public List<String> getPropertyNamesByTableName(String table) {
		List<String> result = Lists.newArrayList();
		Map<String,String> map=Maps.newHashMap();
		map.put("tableName", table);
		map.put("flag", "1");
		List<Config> list = mao.get(map);
		for(Config c : list){
			result.add(fieldToProperty(c.getPropName()));
		}
		return result;
	}
*/
}
